package com.example.administrator.managesystem.video;

import android.content.Context;
import android.util.Log;

import com.example.administrator.managesystem.bean.AddCameraSeriesBean;
import com.example.administrator.managesystem.bean.AddLandBean;
import com.example.administrator.managesystem.bean.CameraListBean;
import com.example.administrator.managesystem.bean.DeleteCameraBean;
import com.example.administrator.managesystem.bean.DeleteLandBean;
import com.example.administrator.managesystem.bean.GetCameraBean;
import com.example.administrator.managesystem.bean.HiKTokenBean;
import com.example.administrator.managesystem.bean.LandListBean;
import com.example.administrator.managesystem.bean.TagBean;
import com.example.administrator.managesystem.bean.UpdateLandBean;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.AddLandView;
import com.example.administrator.managesystem.utils.ToastUtils;
import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class  AddLandPresenter {
    private AddLandView addLandView;

    public AddLandPresenter(AddLandView addLandView) {
        this.addLandView = addLandView;
    }

    public void goodsTypeRequest(final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).goodsTypeRequest()
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<TagBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(TagBean tagBean) {
                    if(tagBean.getStatus()==200){
                     addLandView.showLandTag(tagBean.getData());
                 }
                    }

                });


    }

    public void addLandSendRequest(String user_id, String token,String name,String goods_id,String location,String imgUrl,final Context context){

        if(imgUrl==null || name==null || goods_id==null ) return;
        File file = new File(imgUrl);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

        ServiceManager.getInstance(context).getService(GuideAPI.class).addLandRequest(user_id,token,name,goods_id,location,body)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<AddLandBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(AddLandBean addLandBean) {
                        if(addLandBean.getStatus()==200){
                            ToastUtils.showToast(context,addLandBean.getData());
                            addLandView.showMsg();
                        }else{
                            ToastUtils.showToast(context,addLandBean.getMsg());
                        }
                    }

                });


    }

    public void landListRequest(String user_id, String token,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).landListRequest(user_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<LandListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                        addLandView.showErrMsg();
                    }

                    @Override
                    public void onNext(LandListBean landListBean) {
                   if(landListBean.getStatus()==200){
                       if(landListBean.getData().isEmpty()){
                           ToastUtils.showToast(context,"您还没有土地哟，赶快添加吧！");
                           addLandView.showErrMsg();
                           return;
                       }
                       addLandView.showLandList(landListBean.getData());

                   }
                    }

                });


    }

    public void addCameraRequest(String user_id, String token,String land_id,String device_number,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).addCameraRequest(user_id,land_id,token,device_number)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<AddCameraSeriesBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                        addLandView.showErrMsg();
                    }

                    @Override
                    public void onNext(AddCameraSeriesBean addCameraSeriesBean) {
                   if(addCameraSeriesBean.getStatus()==200){
                       ToastUtils.showToast(context,addCameraSeriesBean.getData());
                       addLandView.updateGrid();

                   }else{
                       ToastUtils.showToast(context,addCameraSeriesBean.getMsg());
                       addLandView.showErrMsg();
                   }
                    }

                });


    }
    public void getCameraRequest(String user_id, String token,String land_id,final Context context){
        Log.e("msg","getCameraBean123:");
        ServiceManager.getInstance(context).getService(GuideAPI.class).getCameraRequest(user_id,land_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<GetCameraBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(GetCameraBean getCameraBean) {
                        if(getCameraBean.getStatus()==200){
                            Log.e("msg","getCameraBean:"+getCameraBean.getData());
                            addLandView.showCameraList(getCameraBean.getData());
                        }

                    }

                });


    }
    public void deleteCameraRequest(String user_id, String token,String land_id,String camera_id,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).deleteCameraRequest(user_id,land_id,camera_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<DeleteCameraBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());

                    }

                    @Override
                    public void onNext(DeleteCameraBean deleteCameraBean) {
                       if(deleteCameraBean.getStatus()==200){
                           ToastUtils.showToast(context,deleteCameraBean.getData());

                       }else{
                           ToastUtils.showToast(context,deleteCameraBean.getMsg());

                       }
                    }

                });
    }
    public void cameraListRequest(String user_id, String token,String land_id,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).cameraListRequest(user_id,land_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<CameraListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(CameraListBean cameraListBean) {
                     if(cameraListBean.getStatus()==200){
                        addLandView.showCameraRecycler(cameraListBean.getData());
                     }

                    }

                });
    }
    public void deleteLandRequest(String user_id, String token, String land_id, final int position, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).deleteLandRequest(user_id,land_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<DeleteLandBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(DeleteLandBean deleteLandBean) {
                     if(deleteLandBean.getStatus()==200){
                         addLandView.showDelete(position);
                     }

                    }

                });
    }

    public void updateLandRequest(String user_id,String token,String name,String land_id,String goods_id,String location,String imgUrl, final Context context){
        if(imgUrl==null || name==null || goods_id==null ) return;
        File file = new File(imgUrl);
        final RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

        ServiceManager.getInstance(context).getService(GuideAPI.class).updateLandRequest(user_id,land_id,token,name,goods_id,location,body)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<UpdateLandBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(UpdateLandBean updateLandBean) {
                       if(updateLandBean.getStatus()==200){
                          addLandView.showMsg();
                       }else{
                           ToastUtils.showToast(context,updateLandBean.getMsg());
                       }
                    }

                });
    }

    public void showHikToken(final Context context, String user_id, String token, final String device_number){
        ServiceManager.getInstance(context).getService(GuideAPI.class).getToken(user_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<HiKTokenBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(HiKTokenBean hiKTokenBean) {
                        if(hiKTokenBean.getStatus()==200){
                            addLandView.showToken(hiKTokenBean.getData(),device_number);
                        }
                    }

                });


    }
}
